package com.stp.cor.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public
class AopProxyUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;

    }

    /**
     * 获取指定类的代理对象，适用于需要事务或其他AOP增强的场景。
     *
     * @param clazz 要获取代理的对象的类
     * @param <T>   泛型标记
     * @return 代理对象实例
     */
    public static <T> T getProxyBean(Class<T> clazz) {
        if (applicationContext == null) {
            throw new IllegalStateException("ApplicationContext not initialized.");
        }
        return applicationContext.getBean(clazz);
    }

    public static Object getProxyBean(String name) {
        return applicationContext.getBean(name);
    }
}
